package com.aep.ProjetoAnalise;

import android.util.Log;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

/**
 * Created with IntelliJ IDEA.
 * User: Christian
 * Date: 09/05/13
 * Time: 19:49
 * To change this template use File | Settings | File Templates.
 * Objeto de manipulação do Feed
 */
public class RSSHandler extends DefaultHandler{
    RSSFeed _feed;
    RSSItem _item;
    int i;
    String _lastElementName = "";
    boolean bFoundChannel = false;
    final int RSS_TITLE = 1;
    final int RSS_LINK = 2;
    final int RSS_DESCRIPTION = 3;
    final int RSS_CATEGORY = 4;
    final int RSS_PUBDATE = 5;

    int depth = 0;
    int currentstate = 0;

    /*
    Construtor
     */
    RSSHandler(){
    }

    /*
    getFeed - retorna o Feed após o parsing do XML
     */
    RSSFeed getFeed(){
        return _feed;
    }

    /*
    Métodos SAX para manipulação do XML
     */
    public void startDocument() throws SAXException
    {
        // inicializa o objeto do tipo Feed que conterá as informações para manipulação
        _feed = new RSSFeed();
        // Objeto auxiliar do tipo RSSItem para manter as informações do channel manipulado
        _item = new RSSItem();

    }
    public void endDocument() throws SAXException
    {
    }
    public void startElement(String namespaceURI, String localName,String qName,
                             Attributes atts) throws SAXException
    {
        depth++;
        if (localName.equals("channel"))
        {
            currentstate = 0;
            return;
        }
        if (localName.equals("image"))
        {
            // grava os dados do feed - aramzena temporariamente em um item
            _feed.setTitle(_item.getTitle());
            _feed.setPubDate(_item.getPubDate());
        }
        if (localName.equals("item"))
        {
            // cria um novo item
            _item = new RSSItem();
            return;
        }
        if (localName.equals("title"))
        {
            currentstate = RSS_TITLE;
            return;
        }
        if (localName.equals("description"))
        {
            currentstate = RSS_DESCRIPTION;
            return;
        }
        if (localName.equals("link"))
        {
            currentstate = RSS_LINK;
            return;
        }
        if (localName.equals("category"))
        {
            currentstate = RSS_CATEGORY;
            return;
        }
        if (localName.equals("pubDate"))
        {
            currentstate = RSS_PUBDATE;
            return;
        }
        currentstate = 0;
    }

    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException
    {
        depth--;
        if (localName.equals("item") && i < 5)
        {
            // adiciona o item a Lista!
            _feed.addItem(_item);
            i++;
            return;
        }
    }

    public void characters(char ch[], int start, int length)
    {
        String theString = new String(ch,start,length);
        Log.i("RSSReader", "characters[" + theString + "]");

        switch (currentstate)
        {
            case RSS_TITLE:
                _item.setTitle(theString);
                currentstate = 0;
                break;
            case RSS_LINK:
                _item.setLink(theString);
                currentstate = 0;
                break;
            case RSS_DESCRIPTION:
                _item.setDescription(theString);
                currentstate = 0;
                break;
            case RSS_CATEGORY:
                _item.setCategory(theString);
                currentstate = 0;
                break;
            case RSS_PUBDATE:
                _item.setPubDate(theString);
                currentstate = 0;
                break;
            default:
                return;
        }

    }
}
